﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using com.eforceglobal.crux.DTOs;
using System.Collections.Generic;
using com.eforceglobal.crux.views.Administration;
using com.eforceglobal.crux.controllers.Administration;
using com.eforceglobal.crux.views.Enums;
using com.eforceglobal.crux.DTOs.Configuration;

public partial class Administration_ManageSubLocations : BaseView, IManageSubLocations 
{
    ManageSubLocationsController _controller = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        _controller = new ManageSubLocationsController(this);
        _controller.InitView(IsPostBack);

    }

    public void grdSubLocations_RowCommand(object sender, CommandEventArgs e)
    {
        RowCommands rowCommand = (RowCommands)Enum.Parse(typeof(RowCommands), e.CommandName);
        if (rowCommand == RowCommands.EditItem)
        {
            string SubLocationId = e.CommandArgument.ToString();
            _controller.OpenSubLocation(int.Parse(SubLocationId));
        }
        else if (rowCommand == RowCommands.DeleteItem)
        {
            string SubLocationId = e.CommandArgument.ToString();
            _controller.DeleteSubLocation(int.Parse(SubLocationId));
        }

     
    }

    protected void grdSubLocations_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton deleteButton = (ImageButton)e.Row.FindControl("btnItemDelete");
            string ReplaceConfirmation = UILiterals.DeleteSubLocationConfirmation.Replace("{0}",
                DataBinder.Eval(e.Row.DataItem, "SubLocationName").ToString());
            deleteButton.Attributes.Add("onclick", "javascript:return "
                + "confirm('" + ReplaceConfirmation + "')");
        }
    }


    #region IManageSubLocations Members

    public List<SubLocationInformation> SubLocations
    {
        get
        {
            return (List<SubLocationInformation>)grdSubLocations.DataSource;
        }
        set
        {
            grdSubLocations.DataSource = value;
            grdSubLocations.DataBind();
        }
    }

    public SubLocationInformation CurrentSubLocation
    {
        get
        {
            SubLocationInformation subLocationInformation = new SubLocationInformation();
            subLocationInformation.SubLocationID = int.Parse(lblSubLocationIDValue.Text);
            subLocationInformation.SubLocationName = txtSubLocationName.Text;
            subLocationInformation.SubLocationAddress = txtSubLocationAddress.Text;
            subLocationInformation.SubLocationComments = txtSubLocationComments.Text;
            subLocationInformation.ParentLocationID = LocationID;
            return subLocationInformation;
        }
        set
        {
            lblSubLocationIDValue.Text = value.SubLocationID.ToString();
            txtSubLocationName.Text = value.SubLocationName;
            txtSubLocationAddress.Text = value.SubLocationAddress;
            txtSubLocationComments.Text = value.SubLocationComments;
        }
    }

    public FormMode FormMode
    {
        get
        {
            if (pnlList.Visible == true)
                return FormMode.List;
            else if (pnlAddEdit.Visible == true)
                return FormMode.AddEdit;
            else
                throw UIExceptions.InvalidFormModeException;
        }
        set
        {
            if (value == FormMode.List)
            {
                pnlList.Visible = true;
                pnlAddEdit.Visible = false;
            }
            else if (value == FormMode.AddEdit)
            {
                pnlList.Visible = false;
                pnlAddEdit.Visible = true;
            }
            else
            {
                throw UIExceptions.InvalidFormModeException;
            }
        }
    }

    #endregion
    protected void btnSave_Click(object sender, EventArgs e)
    {
        _controller.SaveSubLocation();

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        _controller.CancelAddEdit();
    }
    protected void btnAddNewSubLocation_Click(object sender, EventArgs e)
    {
        _controller.OpenSubLocation(0);
    }

    #region IManageSubLocations Members


    public ErrorInformation ErrorDetails
    {
        get
        {
            return pnlError.ErrorInformation;
        }
        set
        {
            pnlError.ErrorInformation = value;
        }
    }

    #endregion

    #region IManageSubLocations Members

    public int LocationID
    {
        get
        {
            return int.Parse(Request.Params["id"].ToString());
        }
    }


    #endregion
}
